package com.robot.mvvm.vm.work

import android.app.Application
import androidx.databinding.ObservableField
import api.IotServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.QuestionDetailDTOBean
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class RepairWorkOrderInfoViewModel(application: Application) : GLBaseViewModel(application) {

    val equipmentName = ObservableField<String>()
    val equipmentNo = ObservableField<String>()
    val contactName = ObservableField<String>()
    val contactPhone = ObservableField<String>()
    val gmtCreateStr = ObservableField<String>()
    val handlerName = ObservableField<String>()
    val questionTypeName = ObservableField<String>()
    val questionDesc= ObservableField<String>()

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val refreshDataEvent = SingleLiveEvent<QuestionDetailDTOBean>()
    }



    fun initData(bean: QuestionDetailDTOBean) {
        equipmentName.set(bean.equipmentName)
        equipmentNo.set(bean.equipmentNo)
        contactName.set(bean.contactName)
        contactPhone.set(bean.contactPhone)
        gmtCreateStr.set(bean.gmtCreateStr)
        handlerName.set(bean.handlerName)
        questionTypeName.set(bean.questionTypeName)
        questionDesc.set(bean.questionDesc)
    }

    fun getQuestionDetail(questionId: String) {
        IotServiceFactory.getQuestionDetail(
            mapOf(
                "questionId" to questionId
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<QuestionDetailDTOBean>>(this) {
            override fun onNext(t: BaseResponse<QuestionDetailDTOBean>) {
                initData(t.data)
                uc.refreshDataEvent.postValue(t.data)
            }
        })
    }
}